#! /usr/bin/env python
"""Utility script to extract all translatable messages from templates and 
macros and write to openlibrary/i18n/messages.pot file.
"""
from __future__ import print_function
import _init_path

import sys
from openlibrary import i18n

def help():
    print("utility to manage i18n messages")
    print() 
    print("USAGE: %s [extract|compile|update]" % sys.argv[0])
    print() 
    print("Available Commands:")
    print("  compile - compile the message files (.po) to .mo")
    print("  extract - extract i18n messages from templates and macros")
    print("  update  - update the existing translations by adding newly added string to it.")
    print("  help    - display this help message")
    
def main(cmd):
    if cmd == 'extract':
        message_sources = [
            'openlibrary/plugins/upstream',
            'openlibrary/templates/type/author',
            'openlibrary/templates/type/doc',
            'openlibrary/templates/type/edition',
            'openlibrary/templates/type/list',
            'openlibrary/templates/type/page',
            'openlibrary/templates/type/user',
            'openlibrary/templates/type/work',
        ]
        template_dir = 'openlibrary/templates/'
        templates = [
            'account', # Main user account UI
            'books',   # books view and edit pages
            'covers',  # author photos and book cover adding UI
            'history', # minimal messaging, but useful
            'home',    # main site pages, review
            'lib',     # needs review
            'lists',
            'merge',
            'publishers',
            'recentchanges',
            'search',  # main search interface
            'site',
            'account.html',
            'diff.html',
            'history.html',
            'internalerror.html',  # Logged internal error
            'login.html',
            'notfound.html',
            'permission_denied.html',
            'subjects.html',
            'widget.html',
            'work_search.html',
        ]
        macro_dir = 'openlibrary/macros/'
        macros = [
            'BookPreview.html',
            'databarHistory.html',
            'databarView.html',
            'databarWork.html',
            'DownloadOptions.html',
            'EditButtons.html',
            'FulltextResults.html',
            'LoanStatus.html',
            'Pager.html',
            'ReadButton.html',
            'SearchResultsWork.html',
            'SocialShare.html',
            'TypeChanger.html',
            'UserEditRow.html',
            'WorldcatLink.html',
        ]
        message_sources += [template_dir + template for template in templates]
        message_sources += [macro_dir + macro for macro in macros]
        i18n.extract_messages(message_sources)
    elif cmd == 'compile':
        i18n.compile_translations()
    elif cmd == 'update':
        i18n.update_translations()
    elif cmd == 'help':
        help()
    else:
        print("unknown command: %s" % repr(cmd), file=sys.stderr)
        sys.exit(2)

if __name__ == "__main__":
    if len(sys.argv) == 2:
        main(sys.argv[1])
    else:
        help()
        sys.exit(1)
